package treasurer;

import home.HomePage;
import includes.Sessions;

import java.io.IOException;
import java.io.Reader;
import java.io.Serializable;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.CheckBox;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import secretary.AddEvent;
import secretary.ManageEvents;
import secretary.SecretaryHome;
import secretary.ManageEvents.CustomCheckBox;
import secretary.ManageEvents.CustomCheckBoxModel;

import com.ibatis.common.resources.Resources;
import com.ibatis.sqlmap.client.SqlMapClient;
import com.ibatis.sqlmap.client.SqlMapClientBuilder;

import controllers.BKCashAdvance;
import controllers.Event;
import controllers.Logs;

public class BookKeepersCashAdvance extends WebPage {
	
	Reader reader = Resources.getResourceAsReader("sqlMapConfig.xml");
	SqlMapClient sqlMap = SqlMapClientBuilder.buildSqlMapClient(reader);
	private DropDownChoice officer;
	private Model dateModel = new Model();
	private Model rcvModel = new Model();
	private Model purposeModel = new Model();
	private Model officerModel = new Model();
	private Model amountModel = new Model();
	private List choice = Arrays.asList(new String[] { "President", "Secretary", "Treasurer" });
	
	
	public BookKeepersCashAdvance() throws IOException, SQLException {
		
		TextField date = new TextField("date", dateModel);
		date.setRequired(true);
		
		TextField rcv = new TextField("rcv", rcvModel);
		rcv.setRequired(true);
		
		TextField purpose = new TextField("purpose", purposeModel);
		purpose.setRequired(true);
		
		officer = new DropDownChoice("officer", officerModel, choice);
		
		TextField amount = new TextField("amount", amountModel);
		amount.setRequired(true);
		
		
		Form form = new Form("bookKeepersForm");
		
		form.add(new Button("add"){
		
		private static final long serialVersionUID = 1L;

		public void onSubmit(){
			
			try {
				String date = (String) dateModel.getObject();
				String rcv = (String) rcvModel.getObject();
				String purpose = (String) purposeModel.getObject();
				String officer = (String) officerModel.getObject();
				String amount = (String) amountModel.getObject();
				
				String uploader = ((Sessions)getSession()).getDesc();
				
				Date today = new Date();
				Timestamp timestamp = new Timestamp(today.getTime());
				
				String ts = timestamp.toString();
				
				System.out.println("date: " + date + " rcv: " + rcv + " purpose: " + purpose + " officer: " + officer + " uploader: " + uploader + "date created" + ts);
				
				BKCashAdvance bk = new BKCashAdvance();
				bk.setDate(date);
				bk.setRcv(rcv);
				int newAmount = Integer.parseInt(amount);
				bk.setAmount(newAmount);
				bk.setOfficer(officer);
				bk.setPurpose(purpose);
				bk.setUploader(uploader);
				bk.setDate_created(ts);
				bk.setStatus("PENDING");
				
				sqlMap.insert("BKCashAdvance.insert", bk);
				
				Logs log = new Logs();
				log.setLog_activity("Book Keepers Cash Advance Form:" + rcv);
				log.setLog_officer(uploader);
				log.setLog_time(ts);
				
				sqlMap.insert("Logs.insert", log);
			
				BookKeepersCashAdvanceList page = new BookKeepersCashAdvanceList();
				setResponsePage(page);
				error("added");
		}
			catch (SQLException e){
				 error("Fill up necessary fields");
			  } catch (IOException e) {
				e.printStackTrace();
			}
		}});
		

		
		add(new BookmarkablePageLink("manageForms", ManageForms.class));
		add(new BookmarkablePageLink("logoutPage", HomePage.class));
		add(new BookmarkablePageLink("home", TreasurerHome.class));
		add(new BookmarkablePageLink("view", BookKeepersCashAdvanceList.class));
		
		
		add(new FeedbackPanel("error"));
		
		form.add(date);
		form.add(rcv);
		form.add(officer);
		form.add(purpose);
		form.add(amount);
		
		add(form);
		
	}
	
}
